**************************************************************
***** 		Probit models 				******
**************************************************************

/*
This replication code generates the findings in the paper
"Populism and De Facto Central Bank Independence"
(Gavin and Manger) and exports the data for
the marginal effects graphs. All code is for Stata version 14.2
unless indicated.

Figures are generated using R 4.1.2. See the included R scripts.

*/

capture log close

		clear all

		macro drop _all
		graph drop _all

* Set this path to work on your own machine

global folder "~/path_to_replication_package"

cd $folder

log using "$folder/DeFactoCBI.log", replace

use "$folder/D01_DeFactoCBI.dta", clear

* largeSample is an indicator variable for the emerging markets sample used in the paper
* while the robustness check in the appendix uses the whole sample

* This is a "basic" model with only the populism variable (1)
probit politicalPressure populism_vparty  t t2 t3 if largeSample == 1, vce(cluster id)
estimates store probit1
est save defactoIv, replace

* Add political controls (2)
probit politicalPressure populism_vparty polity2 electionRunup imfCbiConditions t t2 t3 if largeSample == 1, vce(cluster id)
estimates store probit2
est save defactoIv, append

* Add economic controls (3)
probit politicalPressure populism_vparty polity2 electionRunup imfCbiConditions debt2Gdp Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3 if largeSample == 1, vce(cluster id)
estimates store probit3
est save defactoIv, append

* Add interaction between populism and economic left-right scale (4)
probit politicalPressure c.populism_vparty##c.economicRightParty polity2 electionRunup imfCbiConditions ///
debt2Gdp Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3 if largeSample == 1, vce(cluster id)
estimates store probit4
est save defactoIv, append

* Add the (uninstrumented gross outflows scaled to GDP) (5)

probit politicalPressure c.populism_vparty##c.economicRightParty LagOutflowPercentGdp polity2 electionRunup imfCbiConditions debt2Gdp ///
Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3 if largeSample == 1, vce(cluster id)
estimates store probit5
est save defactoIv, append

margins, at(LagOutflowPercentGdp = (151 (100) 900))

* export the marginal effect on the prediction and the confidence intervals (delta method)
* for use with ggplot in R

probit politicalPressure c.populism_vparty##c.economicRightParty LagOutflowPercentGdp polity2 electionRunup imfCbiConditions debt2Gdp ///
Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3 if largeSample == 1, vce(cluster id)

margins, at(populism_vparty = (0(0.1)1) economicRightParty = (-3.5 (1) 3.5)) post
putexcel set "$folder/interactionmargins.xlsx", replace
putexcel A1=matrix(r(table))

preserve
keep if largeSample == 1
keep populism_vparty
export delimited using "$folder/PopulismHistogramData.csv", replace
restore

probit politicalPressure c.populism_vparty##c.economicRightParty LagOutflowPercentGdp polity2 electionRunup imfCbiConditions debt2Gdp Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3 if largeSample == 1, vce(cluster id)
quietly margins, at(populism_vparty = (0(0.1)1) economicRightParty   = (-3.5(0.1)3.5)) saving(predictions, replace)

preserve
use predictions, clear
rename _at1 Populism
rename _at2 economicRightParty
rename _margin Pr_Pressure
twoway (contour Pr_Pressure Populism economicRightParty), ytitle(Populism) xtitle(Economic left-right-scale) ztitle(Predicted probability of pressure on central bank) ///
xlabel(-3.5(1)3.5)
graph export "$folder/G03Contourplot.pdf", replace

restore

* Pairwise comparison

quietly: probit politicalPressure c.populism_vparty##c.economicRightParty LagOutflowPercentGdp  polity2 electionRunup imfCbiConditions debt2Gdp Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3 if largeSample == 1, vce(cluster id)
margins, at(populism_vparty = (0 (0.5) 1) economicRightParty = (-3.5(1)3.5)) pwcompare(effects) mcompare(sidak)

* Poisson count model, with the count of pressure events in the last quarter as dependent variable (6)
poisson polPressureCount c.populism_vparty##c.economicRightParty polity2 electionRunup LagOutflowPercentGdp imfCbiConditions debt2Gdp Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3 if largeSample == 1, vce(cluster id)
estimates store poisson1
est save defactoIv, append

* LPM full model. (7)
ivreg2 politicalPressure c.populism_vparty##c.economicRightParty (LagOutflowPercentGdp = iv_flows) ///
polity2 electionRunup imfCbiConditions debt2Gdp Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3 if largeSample == 1, cluster(id)
estimates store ivreg22SLS
est save defactoIv, append

margins, at(populism_vparty = (0(0.1)1) economicRightParty = (-3.5 (1) 3.5)) post noestimcheck
putexcel set "$folder/iv_interactionmargins.xlsx", replace
putexcel A1=matrix(r(table))

* Conduct an Anderson-Rubin (1949) test that is robust to autocorrelation and heteroskedasticity
* and check if the effect of the endogenous variable (LagOutflowPercentGdp) on Pressure is equal to zero
* If you don't have this installed, type net search rivtest
ivreg2 politicalPressure c.populism_vparty##c.economicRightParty (LagOutflowPercentGdp = iv_flows) ///
polity2 electionRunup imfCbiConditions debt2Gdp Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3 if largeSample == 1, cluster(id)

rivtest

* only the manually coded observations (8)

xtset id quarterlydate

probit politicalPressure c.populism_vparty##c.economicRightParty LagOutflowPercentGdp polity2 electionRunup imfCbiConditions debt2Gdp Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3 if largeSample == 1 & manual == 1, vce(cluster id)
estimates store manualonly
est save defactoIv, append

* Table 3: Probit and IV-Probit Models

esttab probit1 probit2 probit3 probit4 probit5 poisson1 ivreg22SLS manualonly using "$folder/deFactoCbiProbitResults.tex", replace nogaps booktabs  ///
stats(p r2_p chi2 ll N N_clust, fmt(%9.2f %9.3f %9.0f %9.0f %9.0f %9.0f) labels("$ Prob > F $" "pseudo-$ R^2 $" "Chi-square" "Log-likelihood" "N" "Number of clusters")) ///
b(3) se(3) nonumbers ///
title() mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)")  ///
noconstant ///
obslast order(populism_vparty economicRightParty c.populism_vparty#c.economicRightParty LagOutflowPercentGdp polity2 ///
electionRunup imfCbiConditions debt2Gdp Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3) ///
addnotes("Huber-White standard errors clustered by country. Stock-Yogo weak ID test critical values at 10 percent" "maximal IV size = 16.38") ///
coeflabels(populism_vparty "Degree of populism" economicRightParty "Economic left-right scale" ///
c.populism_vparty#c.economicRightParty "Populism $\times$ Economic left-right scale" ///
LagOutflowPercentGdp "Lagged capital outflows to GDP"  ///
polity2 "Democracy" imfCbiConditions "IMF CB conditions" ///
electionRunup "Less than 12 months to election" debt2Gdp "Debt to GDP ratio" ///
Lag3lnCpi "Lagged inflation" DifflnExchRate "Lagged $ \Delta $ exchange rate" Lag3DifflnGdp "Lagged $ \Delta $ GDP" t " $ t$ " t2 "  $ t^2 $" t3 " $ t^3 $ ")

* Table 4:

* Multinomial probit model, with 0 (no pressure) as base category, 1 pressure, 2 irregular governor turnover, and 3 weakening of central bank independence

mprobit threeOutcomes c.populism_vparty##c.economicRightParty polity2 electionRunup imfCbiConditions debt2Gdp ///
LagOutflowPercentGdp Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3 if largeSample == 1, vce(cluster id) base(0)
estimates store mprobit1
est save defactoIv, append

esttab mprobit1 using "$folder/deFactoCbi-MProbitResults.tex", replace nogaps booktabs  ///
stats(p r2_p chi2 ll N N_clust, fmt(%9.2f %9.3f %9.0f %9.0f %9.0f %9.0f) labels("$ Prob > F $" "pseudo-$ R^2 $" "Chi-square" "Log-likelihood" "N" "Number of clusters")) ///
b(3) se(3) nonumbers noconstant mtitles ///
obslast order(populism_vparty economicRightParty c.populism_vparty#c.economicRightParty LagOutflowPercentGdp polity2 ///
electionRunup imfCbiConditions debt2Gdp Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3) ///
addnotes("Multinomial probit model with Huber-White standard errors clustered by country, base category is 'no pressure'") ///
coeflabels(populism_vparty "Degree of populism" economicRightParty "Economic left-right scale" ///
c.populism_vparty#c.economicRightParty "Populism $\times$ Economic left-right scale" ///
LagOutflowPercentGdp "Lagged capital outflows to GDP" ///
polity2 "Democracy" imfCbiConditions "IMF CB conditions" ///
electionRunup "Less than 12 months to election" debt2Gdp "Debt to GDP ratio" ///
Lag3lnCpi "Lagged inflation" DifflnExchRate "Lagged $ \Delta $ exchange rate" Lag3DifflnGdp "Lagged $ \Delta $ GDP" t " $ t$ " t2 "  $ t^2 $" t3 " $ t^3 $ ")

* Pairwise comparisons of marginal effects for different values of populism and economic left-right scale,
* with Šidák adjustment for multiple comparisons

margins, at(populism_vparty = (0 (0.5) 1) economicRightParty = (-3.5(1)3.5)) predict(outcome(1)) pwcompare(effects) mcompare(sidak)
margins, at(populism_vparty = (0 (0.5) 1) economicRightParty = (-3.5(1)3.5)) predict(outcome(2)) pwcompare(effects) mcompare(sidak)
margins, at(populism_vparty = (0 (0.5) 1) economicRightParty = (-3.5(1)3.5)) predict(outcome(3)) pwcompare(effects) mcompare(sidak)


* Calculate the marginal effects shown in Figure 6. For panel 1 we calculate them for pressure because here the comparisons just above
* indicate a statistically significant difference of margins across different values of both variables

quietly: mprobit threeOutcomes c.populism_vparty##c.economicRightParty polity2 electionRunup imfCbiConditions debt2Gdp ///
LagOutflowPercentGdp Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3 if largeSample == 1, vce(cluster id) base(0)

margins, at(populism_vparty = (0(0.1)1) economicRightParty = (-3.5 (1) 3.5)) predict(outcome(1)) post
putexcel set mprobit1margins, replace
putexcel A1=matrix(r(table))


forvalues i = 2/3 {
	quietly: mprobit threeOutcomes c.populism_vparty##c.economicRightParty polity2 electionRunup imfCbiConditions debt2Gdp ///
	LagOutflowPercentGdp Lag3lnCpi DifflnExchRate Lag3DifflnGdp t t2 t3 if largeSample == 1, vce(cluster id) base(0)
	margins, at(populism_vparty = (0 (0.1) 1)) predict(outcome(`i')) post
	preserve
	gen dydx_pressure = .
	gen dydx_pressure_ll = .
	gen dydx_pressure_ul = .
	matrix T = r(table)
	forvalues bees = 1/11 {
	replace dydx_pressure = T[1, `bees'] if _n == `bees'
	replace dydx_pressure_ll = T[5, `bees'] if _n == `bees'
	replace dydx_pressure_ul = T[6, `bees'] if _n == `bees'
	 }
	keep dydx_pressure dydx_pressure_ll dydx_pressure_ul
	gen populism_steps = (_n/10)-0.1
	drop if _n > 11
	export delimited using "$folder/Mprobit-CIs-PressureOutcome-interaction`i'.csv", replace
	restore
}

/*
Summary statistics: Appendix Table A1
*/
keep if largeSample == 1

estpost tabstat politicalPressure polPressureCount threeOutcomes populism_vparty economicRightParty polity2 electionRunup imfCbiConditions ///
LagOutflowPercentGdp debt2Gdp ///
Lag3lnCpi DifflnExchRate Lag3DifflnGdp, listwise ///
statistics(N mean sd min max) columns(statistics)
eststo sumstats

esttab sumstats using "$folder/deFactoCbi-summarystats.tex",  replace ///
nogaps booktabs nonumber  nonote noobs ///
cell((count(fmt(0)) mean(fmt(3)) sd(fmt(3)) min(fmt(3)) max(fmt(3)))) ///
coeflabels(politicalPressure "Political pressure" polPressureCount "Pressure incidents per quarter" ///
populism_vparty "Degree of populism" LagOutflowPercentGdp "Lagged capital outflows to GDP" ///
polity2 "Democracy" imfCbiConditions "IMF CB conditions" ///
electionRunup "Less than 12 months to election" debt2Gdp "Debt to GDP ratio" ///
Lag3lnCpi "Lagged inflation" DifflnExchRate "Lagged $ \Delta $ exchange rate" Lag3DifflnGdp "Lagged $ \Delta $ GDP") ///
collabels("N" "Mean" "SD" "Min" "Max" "N")

log close
